<template>
  <el-container>
    <el-header
      style="background-color: #363636; height: 60px; padding-top: 10px"
    >
      <el-row>
        <el-button
          type="text"
          @click="home()"
          style="
            color: #409eff;
            float: left;
            margin-left: 20px;
            font-size: 20px;
          "
          >Home</el-button
        >
        <el-button
          type="text"
          @click="logout()"
          style="color: red; float: right; margin-right: 20px"
          >注销</el-button
        >
        <el-button
          type="text"
          @click="like()"
          style="color: white; float: right; margin-right: 20px"
          >购物车</el-button
        >
      </el-row>
    </el-header>
    <el-main style="width: 80%; margin-left: 10%">
      <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
      >
        <el-menu-item index="1">冰箱</el-menu-item>
        <el-menu-item index="2">电视</el-menu-item>
        <el-menu-item index="3">空调</el-menu-item>
        <el-menu-item index="4">洗衣机</el-menu-item>
      </el-menu>
      <el-row class="row-bg" style="margin-bottom: 10px; margin-top: 10px">
        <el-col :span="2" style="float: right">
          <el-button
            type="primary"
            plain
            icon="el-icon-search"
            size="medium"
            @click="search()"
            >搜索</el-button
          >
        </el-col>
        <el-col :span="6" style="float: right">
          价格区间:
          <el-input
            v-model="price_1"
            placeholder=""
            size="medium"
            style="margin-left: 10px; width: 80px"
          ></el-input>
          -
          <el-input
            v-model="price_2"
            placeholder=""
            size="medium"
            style="width: 80px"
          ></el-input>
        </el-col>
        <el-col :span="6" style="float: right">
          房屋尺寸:
          <el-input
            v-model="houseArea"
            placeholder="平米"
            size="medium"
            style="margin-left: 10px; width: 150px"
          ></el-input>
        </el-col>
      </el-row>
      <el-row v-for="ps in products" type="flex">
        <el-col
          v-for="product in ps"
          :key="product.price"
          style="margin: 5px; width: 25%"
        >
          <el-card :body-style="{ padding: '0px' }">
            <div style="padding: 14px">
              <img
                :src="product.pic"
                class="image"
                @click="selectItem(product)"
              />
              <div 
              @click="selectItem(product)"
              >
                <span>{{ product.stitle }}</span>
                <br />
                <span style="color: #cd2626">￥ {{ product.price }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-main>
  </el-container>
</template>

<script>
import store from "../store";

export default {
  name: "Home",
  data() {
    return {
      houseArea: "",
      price_1: "",
      price_2: "",
      products: [],
      activeIndex: "1",
    };
  },
  mounted: function () {
    this.search(); //页面加载时自动执行
  },
  methods: {
    like() {
      this.$router.push({
        name: "Like",
      });
    },
    selectItem(item) {
      this.$router.push({
        name: "Detail",
        params: {
          product: item,
        },
      });
    },
    handleSelect(key, keyPath) {
      this.activeIndex = key.toString();
      this.search();
    },
    home() {
      this.$router.push("/home");
    },
    logout() {
      this.$router.push("/");
    },
    search() {
      this.$apiFun
        .requestData({
          type: this.activeIndex,
          area: this.houseArea,
          price_start: this.price_1,
          price_end: this.price_2,
        })
        .then((response) => {
          if (response["success"]) {
            this.products.splice(0, this.products.length);
            var list = response.data;
            var r = list.length / 4;
            for (var i = 0; i < r; i++) {
              var index = i * 4;
              var l = 4;
              var ps = [];
              for (; index < list.length; index++) {
                var p = list[index];
                var maxLen = 10;
                p.stitle = p.title
                if (p.title && p.title.length > maxLen)
                  p.stitle = p.title.substring(0, maxLen) + "...";
                ps.push(p);
                l--;
                if (l == 0) {
                  break;
                }
              }
              this.products.push(ps);
            }
            this.products.splice(0, 0);
          } else {
            alert(response["msg"]);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
<style>
.image {
  width: 200px;
  height: 200px;
}
</style>
